'use strict';

/**
 * @ngdoc overview
 * @name payPcApp
 * @description
 * # payPcApp
 *
 * Main module of the application.
 */
angular
    .module('payPcApp', [
        'ngAnimate',
        'ngCookies',
        'ngResource',
        'ngRoute',
        'ngSanitize',
        'ngTouch',
        'ui.router',
        'ui.tree'
    ])
    .constant('AppConfig', {
        WEB_ROOT: 'http://115.29.37.191:8080/Library/library/',
        universityId: 'gh_0b2f868ef759',
        data: {},
        get_login_identity: function() {
            return this.data.userId ? this.data.userId : localStorage['APP_userId'];

        },
        get_time: function() {
            //return this.data['.expires'] ? this.data['.expires'] : localStorage['APP_.expires'];
        },
        get_refresh_token: function() {
            //return this.data.refresh_token ? this.data.refresh_token : localStorage['APP_refresh_token'];
        },
        is_login: function() {
            if (this.get_login_identity() != '' && this.get_login_identity() != undefined) {
                return true;
            }
            return false;
        },
        logout: function() {
            this.data = {};
            localStorage.removeItem("APP_userId");
        }
    })
    .run(['$rootScope', '$location', 'AppConfig', 'loginservice', 'AuthService', function($rootScope, $location, AppConfig, loginservice, AuthService) {
        $rootScope.refresh = false;
        $rootScope.$on('$stateChangeStart',
            function(event, toState, toParams, fromState, fromParams) {
                console.log(fromState);
                console.log(toState);
                console.log(AppConfig.get_login_identity());

                if (toState.name == 'login' && (AppConfig.get_login_identity() != '' && AppConfig.get_login_identity() != undefined)) {
                    $location.replace('/mylib');
                    console.log('mylib');
                }
                if (toState.name == 'mylib' && (AppConfig.get_login_identity() == '' || AppConfig.get_login_identity() == undefined)) {
                    console.log(' no login');
                    $location.path('/login');
                }
                if (!AuthService.hasAuth(toState.name)) {
                    //$location.path('/');
                }
            });
        /*        $rootScope.$watch('refresh', function(newValue, oldValue) {
                    if (newValue) {
                        loginservice.updatedata();
                        $rootScope = false;
                    }

                });*/
    }])
    .config(function($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('booksearch', {
                url: '/booksearch',
                views: {
                    "header": {
                        templateUrl: "views/header.html",
                        controller: 'HeaderCtrl'
                    },
                    "container": {
                        templateUrl: "views/booksearch.html",
                        controller: 'booksearchCtrl'
                    },
                    "footer": {
                        templateUrl: "views/footer.html",
                    }
                }
            })
            .state('bookdetail', {
                url: '/bookdetail',
                views: {
                    "header": {
                        templateUrl: "views/header.html",
                        controller: 'HeaderCtrl'
                    },
                    "container": {
                        templateUrl: "views/bookdetail.html",
                        controller: 'bookdetailCtrl'

                    },
                    "footer": {
                        templateUrl: "views/footer.html",
                    }
                }
            })
            .state('journal', {
                url: '/journal',
                views: {
                    "header": {
                        templateUrl: "views/header.html",
                        controller: 'HeaderCtrl'
                    },
                    "container": {
                        templateUrl: "views/journal.html",
                    },
                    "footer": {
                        templateUrl: "views/footer.html",
                    }
                }

            })
            .state('hotlend', {
                url: '/hotlend',
                views: {
                    "header": {
                        templateUrl: "views/header.html",
                        controller: 'HeaderCtrl'
                    },
                    "container": {
                        templateUrl: "views/hotlend.html",
                        controller: 'hotlendCtrl'
                    },
                    "footer": {
                        templateUrl: "views/footer.html",
                    }
                }

            })
            .state('classify', {
                url: '/classify',
                views: {
                    "header": {
                        templateUrl: "views/header.html",
                        controller: 'HeaderCtrl'
                    },
                    "container": {
                        templateUrl: "views/classify.html",
                        controller: 'classifyCtrl'
                    },
                    "footer": {
                        templateUrl: "views/footer.html",
                    }
                }

            })
            .state('newbook', {
                url: '/newbook',
                views: {
                    "header": {
                        templateUrl: "views/header.html",
                        controller: 'HeaderCtrl'
                    },
                    "container": {
                        templateUrl: "views/newbook.html",
                        controller: 'newbookCtrl'
                    },
                    "footer": {
                        templateUrl: "views/footer.html",
                    }
                }

            })
            .state('mylib', {
                url: '/mylib',
                views: {
                    "header": {
                        templateUrl: "views/header.html",
                        controller: 'HeaderCtrl'
                    },
                    "container": {
                        templateUrl: "views/mylib.html",
                        controller: 'mylibCtrl'
                    },
                    "footer": {
                        templateUrl: "views/footer.html",
                    }
                }

            })
            .state('login', {
                url: "/login",
                views: {
                    "header": {
                        templateUrl: "views/header.html",
                        controller: 'HeaderCtrl'
                    },
                    "container": {
                        templateUrl: "views/login.html",
                        controller: 'loginCtrl'
                    },
                    "footer": {
                        templateUrl: "views/footer.html",
                    }
                }
            });
        $urlRouterProvider.otherwise('/booksearch');
        // .otherwise({
        //     redirectTo: '/'
        // });
    });